
local module = _G["BlinkHealthTextModule"]

if select(2, UnitClass("player")) ~= "PALADIN" or not module then return end

local tcopy
local config_added = false
local enabled_the_art_of_war = false

local L_GRAND_CRUSADER = "위대한 성전사"
local L_AVENGERS_SHIELD = "응징의 방패" -- id 31935
local L_THE_ART_OF_WAR = "전쟁의 기술" -- id 87138
local L_LIGHT_OF_DAWN = "여명의 빛" -- id 85222
local L_TEMPLARS_VERDICT = "기사단의 선고" -- id 85256
local L_SHIELD_OF_THE_RIGHTEOUS = "정의의 방패" -- id 53600
local L_DIVINE_PURPOSE = "신성한 목적" -- id 86172
local L_EXORCISM = "퇴마술"
local L_PALADIN_CONFIG = "성기사 설정"
local L_USE_COMBO = "성기사 신성한 힘의 갯수를 표시합니다."
local L_USE_3HOLY_POWER_SKILL = "전문화별로 신성한 힘이 3개 이상일때 대표 스킬을 표시해줍니다."
local L_USE_3HOLY_POWER_SKILL_DESC = "전문화별로 신성한 힘이 3개 이상일때 대표 스킬을 표시해줍니다.|n신성 |TInterface\\Icons\\Spell_Paladin_Lightofdawn:20|t여명의 빛|n보호 |TInterface\\Icons\\Ability_Paladin_Shieldofvengeance:20|t정의의 방패|n징벌 |TInterface\\Icons\\Spell_Paladin_Templarsverdict:20|t기사단의 선고"


local defaultDB = {
	db_ver = 1.0,
	use_combo = true,
	use_3holy_power_skill = true,
}

module.config = {
	[L_GRAND_CRUSADER] = {
		spellid = 85043
	},
	[L_THE_ART_OF_WAR] = {
		spellid = 87138,
		event = {"SPELL_ACTIVATION_OVERLAY_SHOW", "SPELL_ACTIVATION_OVERLAY_HIDE"}
	},
	[L_TEMPLARS_VERDICT] = {
		spellid = 85256
	},
	[L_SHIELD_OF_THE_RIGHTEOUS] = {
		spellid = 53600
	},
	[L_DIVINE_PURPOSE] = {
		spellid = 86172,
		--talent = 15
	},
	[L_EXORCISM] = {
		spellid = 879
	},
}

-------------------------------------------------------------------------------
-- local functions
-------------------------------------------------------------------------------

function tcopy(to, from) -- "to" must be a table (possibly empty)
	for k,v in pairs(from) do
		if(type(v)=="table") then
			if not to then to = {} end
			to[k] = {}
			tcopy(to[k], v)
		else
			to[k] = v
		end
	end
end

local function getSize(baseSize, expirationTime, duration)
	local size = (expirationTime-GetTime()) / duration
	if size < 0.3 then
		size = baseSize * .8
	elseif size < 0.6 then
		size = baseSize * 1.0
	else
		size = baseSize * 1.2
	end
	return size / 2
end


-------------------------------------------------------------------------------
-- module functions
-------------------------------------------------------------------------------

function module:init()
	if self.addon.db then
		if not self.addon.db.class or not self.addon.db.class.db_ver or self.addon.db.class.db_ver < defaultDB.db_ver then
			self.addon.db.class = {}
			tcopy(self.addon.db.class, defaultDB)
		end
	end

	self.addon.mainFrame:RegisterEvent("PLAYER_ALIVE")
	self.addon.mainFrame:RegisterEvent("LEARNED_SPELL_IN_TAB")
	self.addon.mainFrame:RegisterEvent("CHARACTER_POINTS_CHANGED")

	self:update()

	if not config_added then
		self:AddMiscConfig{
			type = "description",
			text = L_PALADIN_CONFIG,
			fontobject = "QuestTitleFont",
			r = 1,
			g = 0.82,
			b = 0,
			justifyH = "LEFT",
		}
		self:AddMiscConfig{
			type = "toggle",
			text = L_USE_COMBO,
			tooltip = L_USE_COMBO,
			get = function ()
				if self.addon and self.addon.db then
					return self.addon.db.class.use_combo
				end
				return
			end,
			set = function (value)
				self.addon.db.class.use_combo = value
			end,
			needRefresh = true,
		}
		self:AddMiscConfig{
			type = "toggle",
			text = L_USE_3HOLY_POWER_SKILL,
			tooltip = L_USE_3HOLY_POWER_SKILL_DESC,
			get = function ()
				if self.addon and self.addon.db then
					return self.addon.db.class.use_3holy_power_skill
				end
				return
			end,
			set = function (value)
				self.addon.db.class.use_3holy_power_skill = value
			end,
			needRefresh = true,
		}
		
		config_added = true
	end
end

function module:update()
	self.primary = GetSpecialization()
	self.talentGroup, self.numTalentGroups = GetActiveSpecGroup(false), GetNumSpecGroups(false)
	for auraName, t in pairs(self.config) do
		if t.talent and t.talent > 0 then
			--local name, iconTexture, tier, column, selected, available = GetTalentInfo(t.talent);
			local spellid, name, iconTexture, selected, available = GetTalentInfo(t.talent);
			if name and available then
				self.config[auraName].use = true
				if t.event and self.addon and self.addon.mainFrame then
					if type(t.event)=='table' then
						for i=1, #t.event do
							self.addon.mainFrame:RegisterEvent(t.event[i])
						end
					else
						self.addon.mainFrame:RegisterEvent(t.event)
					end
				end
			end
		else
			self.config[auraName].use = false
			if IsPlayerSpell(t.spellid) then
				self.config[auraName].use = true
				if t.event and self.addon and self.addon.mainFrame then
					if type(t.event)=='table' then
						for i=1, #t.event do
							self.addon.mainFrame:RegisterEvent(t.event[i])
						end
					else
						self.addon.mainFrame:RegisterEvent(t.event)
					end
				end
			end
		end
	end
end

function module:LEARNED_SPELL_IN_TAB(...)
	self:update()
end
module.CHARACTER_POINTS_CHANGED = module.LEARNED_SPELL_IN_TAB
function module:PLAYER_ALIVE(...)
	self.addon.mainFrame:UnregisterEvent("PLAYER_ALIVE")
	self:update()
end
function module:SPELL_ACTIVATION_OVERLAY_SHOW(...)
	local spellID = ...
	if spellID == 59578 then
		enabled_the_art_of_war = true
	end
end
function module:SPELL_ACTIVATION_OVERLAY_HIDE(...)
	local spellID = ...;
	if spellID == 59578 then
		enabled_the_art_of_war = false
	end
end
function module:getPlayerText()
	if self.addon.db.class.use_combo then
		local numHolyPower = UnitPower( "player", SPELL_POWER_HOLY_POWER )
		if numHolyPower > 0 then
			return (":|cff%02x%02x%02x%d|r"):format(255, 255, 63, numHolyPower)
		end
	end
	return ""
end

function module:getTargetText()
	local text = ""
	local size = self.addon.db.fontSizeHealth
	local name, rank, icon, count, debufType, duration, expirationTime
	local r, g, b = 1, 1, 1


	if self.addon.db.class.use_3holy_power_skill then
		local already_display_holypower_spell = false
		if self.config[L_DIVINE_PURPOSE].use then
			name, rank, icon, count, debufType, duration, expirationTime = UnitAura("player", L_DIVINE_PURPOSE, nil, "HELPFUL")
			if name then
				if self.primary == 1 then
					icon = "Interface\\Icons\\Spell_Paladin_Lightofdawn"
				elseif self.primary == 2 then
					icon = "Interface\\Icons\\Ability_Paladin_Shieldofvengeance"
				elseif self.primary == 3 then
					icon = "Interface\\Icons\\Spell_Paladin_Templarsverdict"
				end
				size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
				text = text .. (":|T%s:%d|t"):format(icon, size)
				already_display_holypower_spell = true
			end
		end

		if already_display_holypower_spell == false then
			if self.primary == 1 then
				local numHolyPower = UnitPower( "player", SPELL_POWER_HOLY_POWER )
				if numHolyPower >= 3 then
					size = self.addon.db.fontSizeHealth / 2
					text = text .. (":|TInterface\\Icons\\Spell_Paladin_Lightofdawn:%d|t"):format(size)
				end
			elseif self.primary == 2 then
				local numHolyPower = UnitPower( "player", SPELL_POWER_HOLY_POWER )
				if numHolyPower >= 3 then
					size = self.addon.db.fontSizeHealth / 2
					text = text .. (":|TInterface\\Icons\\Ability_Paladin_Shieldofvengeance:%d|t"):format(size)
				end
			elseif self.primary == 3 then
				local numHolyPower = UnitPower( "player", SPELL_POWER_HOLY_POWER )
				if numHolyPower >= 3 then
					size = self.addon.db.fontSizeHealth / 2
					text = text .. (":|TInterface\\Icons\\Spell_Paladin_Templarsverdict:%d|t"):format(size)
				end
			end
		end
	end

	if self.config[L_GRAND_CRUSADER].use then
		name, rank, icon, count, debufType, duration, expirationTime = UnitAura("player", L_GRAND_CRUSADER, nil, "HELPFUL")
		if name then
			size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
			text = text .. (":|TInterface\\Icons\\Spell_Holy_Avengersshield:%d|t"):format(size)
		end
	end

	if self.config[L_THE_ART_OF_WAR].use and enabled_the_art_of_war==true then
		if icon then
			size = self.addon.db.fontSizeHealth / 2
			text = text .. (":|TInterface\\Icons\\Spell_Holy_Excorcism_02:%d|t"):format(size)
		end
	end

	return text
end
